package org.black.missedCall2;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.provider.ContactsContract;
import android.support.v4.app.DialogFragment;

public class MissedCallRowMenuDialogFragment extends DialogFragment {
	public interface MissedCallRowMenuDialogListener {
		public void onChoosingDeleteSpecificRecord(DialogFragment dialog,
				int position);

		public void onChooseingDeleteAllRecordFromTheSameNumber(
				DialogFragment dialog, String phoneNumber);
	}

	private MissedCallRowMenuDialogListener missedCallRowMenuDialogListener;

	private String phoneNumber;
	private int rowPosition;
	private long contactId;

	public Dialog onCreateDialog(Bundle savedInstanceState) {
		super.onCreateDialog(savedInstanceState);
		this.phoneNumber = getArguments().getString(
				MissedCallConstant.PHONE_NUMBER);
		this.rowPosition = getArguments().getInt(
				MissedCallConstant.ROW_POSITION);
		this.contactId = getArguments().getLong(MissedCallConstant.CONTACT_ID);

		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		DialogInterface.OnClickListener clickListener = new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				switch (which) {
				case 0:
					missedCallRowMenuDialogListener
							.onChooseingDeleteAllRecordFromTheSameNumber(
									MissedCallRowMenuDialogFragment.this,
									phoneNumber);
					break;
				case 1:
					missedCallRowMenuDialogListener
							.onChoosingDeleteSpecificRecord(
									MissedCallRowMenuDialogFragment.this,
									rowPosition);
					break;
				case 2:
					if (contactId > -1) {
						Intent intent = new Intent(Intent.ACTION_VIEW,
								Uri.withAppendedPath(
										ContactsContract.Contacts.CONTENT_URI,
										String.valueOf(contactId)));
						MissedCallRowMenuDialogFragment.this
								.startActivity(intent);
					} else if (MissedCallConstant.UNKNOWN_PHONE_NUMBER_VALUE
							.equals(phoneNumber) == false) {
						Intent intent = new Intent(Intent.ACTION_INSERT);
						intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
						intent.putExtra(ContactsContract.Intents.Insert.PHONE,
								phoneNumber);
						MissedCallRowMenuDialogFragment.this
								.startActivity(intent);
					}
					break;
				}

			}
		};
		if (contactId > -1) {
			builder.setItems(R.array.missed_call_row_menu_with_contact,
					clickListener);
		} else if (MissedCallConstant.UNKNOWN_PHONE_NUMBER_VALUE
				.equals(phoneNumber) == false) {
			SharedPreferences sharedPreferences = PreferenceManager
					.getDefaultSharedPreferences(getActivity());
			boolean allowAddCallsToContacts = sharedPreferences
					.getBoolean(
							getString(R.string.allow_add_calls_to_contacts_preference_key),
							false);
			int menuId = R.array.missed_call_row_menu_unknown_number;

			if (allowAddCallsToContacts == true) {
				menuId = R.array.missed_call_row_menu_without_contact;
			}
			builder.setItems(menuId, clickListener);
		} else {
			builder.setItems(R.array.missed_call_row_menu_unknown_number,
					clickListener);

		}
		return builder.create();
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		try {
			missedCallRowMenuDialogListener = (MissedCallRowMenuDialogListener) activity;
		} catch (ClassCastException e) {
			throw new ClassCastException(activity.toString()
					+ " must implement MissedCallRowMenuDialogListener");
		}
	}
}
